package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.maiji.cloud.entities.shopingmall.DiscountCouponLog;
import com.maiji.cloud.entities.shopingmall.TaskEntity;
import com.maiji.cloud.entities.shopingmall.TaskLog;
import com.maiji.cloud.mapper.TaskMapper;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.ResMetaData;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.service.TaskService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TaskServiceImpl extends ServiceImpl<TaskMapper, TaskEntity> implements TaskService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public BaseMetaResDto<List<TaskEntity>> findAllTasks(ReqMetaData metaData) {
        Page<TaskEntity> page = selectPage(new Page<>(metaData.getPage(), metaData.getSize()), new EntityWrapper<>());
        ResMetaData resMetaData = new ResMetaData(page.getCurrent(), page.getSize(), page.getTotal());
        return new BaseMetaResDto(Status.SUCCESS).setData(page.getRecords()).setResMetaData(resMetaData);
    }

    @Override
    public BaseResDto editTask(TaskLog taskLog) {
        taskLog.setEditDate(new Date());
        TaskEntity taskEntity = new TaskEntity();
        BeanUtils.copyProperties(taskLog, taskEntity);
        if (! updateById(taskEntity)) return new BaseResDto(Status.ERROR);
        mongoTemplate.save(taskLog.setOperationDesc("修改规则"), TaskLog.TABLE_NAME);
        return new BaseResDto(Status.SUCCESS);
    }

    @Override
    public BaseMetaResDto<List<TaskLog>> findAllTaskLogs (List<Date> updateDates, ReqMetaData metaData) {
        Query query = new Query().with(new Sort(new Sort.Order(Sort.Direction.DESC,"editDate")));
        if (updateDates != null) {
            Date startDate = updateDates.size() > 0 ? updateDates.get(0): null;
            Criteria updateDateCriteria = Criteria.where("editDate");
            Boolean updFlag = false;
            if (startDate != null) {
                updateDateCriteria = updateDateCriteria.gte(startDate);
                updFlag = true;
            }
            Date endDate = updateDates.size() > 1 ? updateDates.get(1): null;
            if (endDate != null) {
                updateDateCriteria = updateDateCriteria.lte(endDate);
                updFlag = true;
            }
            if (updFlag) query.addCriteria(updateDateCriteria);
        }
        Long total = mongoTemplate.count(query, DiscountCouponLog.TABLE_NAME);
        query.skip((metaData.getPage() - 1) * metaData.getSize()).limit(metaData.getSize());
        List<TaskLog> taskLogs = mongoTemplate.find(query, TaskLog.class, TaskLog.TABLE_NAME);
        ResMetaData resMetaData = new ResMetaData(metaData.getPage(), metaData.getSize(), total);
        return new BaseMetaResDto<List<TaskLog>>(Status.SUCCESS).setData(taskLogs).setResMetaData(resMetaData);
    }

}
